﻿#include <iostream>
#include <SignalSlotTrivial.h>
#include <SignalSlot.h>
#include <functional>
#include <string>

using namespace std;

class Foo : public NonCopyable
{
public:
    void zero();
    void one(int);
    void oner(int&);
    void oneString(const std::string& str);
    int m_nid;
    virtual void printB()
    {
        cout<<"printB"<<endl;
    }
};

void Foo::zero()
{
    printf("Foo::zero()\n");
}

void Foo::one(int x)
{
    printf("Foo::one() x=%d\n", x);
}

void Foo::oner(int &x)
{
    printf("Foo::one() x=%d\n", x);
}

void Foo::oneString(const std::string& str)
{
    std::cout<<"tc test case 001 : "<<str<<std::endl;
}

void x1()
{
    std::cout<<"tc test case 005"<<std::endl;
}

void x2(int n)
{
    std::cout<<"tc test case 006 "<<n<<std::endl;
}

int main()
{
//    SignalTrivial<int()> signal1;
//    Foo f;
//    signal1.connect(std::bind(&Foo::zero, &f));
//    signal1.connect(std::bind(&Foo::one, &f, 42));
//    signal1.connect(std::bind(&Foo::oneString,&f,"good"));
//    signal1.call();

//    SignalTrivial<int(int)> signal2;
//    // 参数占位，可能代表暂时绑定一个未知变量，最后调用时才传入
//    signal2.connect(std::bind(&Foo::one,&f,std::placeholders::_1));
//    signal2.connect(std::bind(&Foo::one,&f,std::placeholders::_1));
//    signal2.call(54);
//    std::cout<<"tc test case 002"<<std::endl;
//    signal2.call(53);

//    SignalTrivial<void(const std::string&)> signal3;
//    signal3.connect(std::bind(&Foo::oneString, &f,std::placeholders::_1));
//    signal3.call("hello");

    muduo::Signal<void()> signal;
    signal.call();
    std::cout<<"tc test case 001"<<std::endl;

    Foo f;
    muduo::Slot s1 = signal.connect(std::bind(&Foo::zero, &f));
    signal.call();
    std::cout<<"tc test case 002"<<std::endl;

    muduo::Slot s2 = signal.connect(std::bind(&Foo::one, &f, 42));
    signal.call();
    std::cout<<"tc test case 003"<<std::endl;

    //创建一个空的智能指针,并赋值给s1,原s1所指的对象，没有holder，导致释放
    s1 = muduo::Slot();
    signal.call();
    std::cout<<"tc test case 004 : "<<s1.use_count()<<std::endl;

    s1 = s2 = muduo::Slot();
    signal.call();
    std::cout<<"tc test case 005"<<std::endl;
    return 0;
}
